home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 May / Macworld (1998-05).dmg / Serious Demos / Lasso 2.5 Test Drive / Lasso 2.5 CGI / Java / LassoProxy / LassoRequest.java < prev    next >
Text File  |  1997-12-12  |  14KB  |  479 lines

  1. /*
  2.     LassoRequest.java
  3.  
  4.     Request parameter to LassoProxy. Add your parameters, then invoke the toString() method
  5.     to generate a URL-encoded file specification string.
  6.     
  7.     The toString() method invokes validate() and catches any exceptions it throws. For
  8.     debugging purposes, you can invoke validate() from your code and catch the exceptions
  9.     yourself to see what's going on. 
  10.     
  11.     Copyright © 1996 Blue World Communications, Inc. All rights reserved.
  12. */
  13.  
  14. import java.util.*;
  15. import java.net.*;
  16. import java.lang.IllegalArgumentException;
  17.  
  18. public class LassoRequest
  19. {
  20.         // Action types
  21.     public final static int     SEARCH         =  0;
  22.     public final static int     ADD            =  1;
  23.     public final static int     UPDATE         =  2;
  24.     public final static int     DELETE         =  3;
  25.     public final static int     FIELD_INFO =  4; // returns field names, types and value lists for the fields
  26.     public final static int     FIND_ALL = 5;
  27.     public final static int    RANDOM = 6;
  28.     
  29.         // Field operator types
  30.     public final static int     EQUALS                 = 0;    // search operators
  31.     public final static int     NOT_EQUALS             = 1;
  32.     public final static int     CONTAINS                = 2;
  33.     public final static int     BEGINS_WITH            = 3;
  34.     public final static int     ENDS_WITH            = 4;
  35.     public final static int     GREATER_THAN            = 5;
  36.     public final static int     GREATER_THAN_EQUALS    = 6;
  37.     public final static int     LESS_THAN            = 7;
  38.     public final static int     LESS_THAN_EQUALS        = 8;    // check validFieldOperator() if defining
  39.                                                         // more search operators
  40.     public final static int     MAX_RECORDS_DEFAULT     = 50;
  41.     public final static int     ALL    = -1;    // for retrieving all records
  42.     
  43.         // Logical operator types
  44.     public final static int     AND     = 0;    // values for fLogicalOperator
  45.     public final static int     OR      = 1;
  46.     
  47.         // Sort orders
  48.     public final static int     ASCENDING      = 0;        // values for fSortOrders
  49.     public final static int     DESCENDING     = 1;
  50.     public final static int     CUSTOM             = 2;        // check validSortOrder() if defining new sort orders
  51.     
  52.     private int        fAction;            // required
  53.     private String     fDatabaseName;        // required
  54.     private String     fLayoutName;            // optional (default to layout 0)
  55.     private int          fSkipRecords;        // for searches (optional)
  56.     private int          fMaxRecords;            // for searches (optional)
  57.     private int        fLogicalOperator;    // for searches (optional)
  58.     private int        fRecordID;            // only needed when acting on a single record
  59.     private int        fTimeout;            // in seconds, for searches (optional)
  60.     
  61.     private Vector    fSearchFieldNames;        // String
  62.     private Vector    fSearchFieldValues;        // String
  63.     private Vector    fSearchFieldOperators;    // Integer
  64.     
  65.     private Vector    fReturnFieldNames;        // String
  66.     
  67.     private Vector    fSortFields;        // String
  68.     private Vector    fSortOrders;        // Integer
  69.     
  70.     private String    fLasso;            // entity to which the request is sent. i.e. lasso.acgi or action.lasso
  71.     
  72.     public LassoRequest(String lasso)        // constructor -- set default/initial values
  73.     {
  74.         fLasso = new String(lasso);
  75.         initFields();
  76.     }
  77.     
  78.     public LassoRequest()                // constructor which sets the fLasso field to the default: action.lasso
  79.     {
  80.         fLasso = new String("action.lasso");
  81.         initFields();
  82.     }
  83.     
  84.     protected void initFields()
  85.     {
  86.         fSearchFieldNames     = new Vector();
  87.         fSearchFieldValues     = new Vector();
  88.         fSearchFieldOperators     = new Vector();
  89.         fSortFields             = new Vector();
  90.         fSortOrders         = new Vector();
  91.         fReturnFieldNames     = new Vector();
  92.         resetAll();
  93.     }
  94.     
  95.     public void resetAll()
  96.     {
  97.         fDatabaseName = null;
  98.         fLayoutName   = null;
  99.         
  100.         resetSearch();
  101.     }
  102.     
  103.     public void resetSearch()
  104.     {
  105.         fAction           = SEARCH;
  106.         fRecordID         = -1;
  107.         fSkipRecords     = 0;
  108.         fMaxRecords      = MAX_RECORDS_DEFAULT;
  109.         fLogicalOperator = AND;
  110.         fTimeout            = 0;
  111.         
  112.         fSearchFieldNames.removeAllElements();
  113.         fSearchFieldValues.removeAllElements();
  114.         fSearchFieldOperators.removeAllElements();
  115.         fReturnFieldNames.removeAllElements();
  116.         resetSort();
  117.     }
  118.     
  119.     public void resetSort()
  120.     {
  121.         fSortFields.removeAllElements();
  122.         fSortOrders.removeAllElements();
  123.     }
  124.     
  125.     public int numSearchFields()
  126.     {    return fSearchFieldNames.size();        }
  127.     
  128.     // set the name of the database use for this request
  129.     public final void setDatabaseName(String name)
  130.     {
  131.         fDatabaseName = new String(name);
  132.     }
  133.     
  134.     public final String databaseName()
  135.     {    return new String(fDatabaseName);    }
  136.     
  137.     public final String layoutName()
  138.     {    return new String(fLayoutName);        }
  139.     
  140.     // set the name of the layout used for this request
  141.     public final void setLayoutName(String name)
  142.     {
  143.         fLayoutName = new String(name);
  144.     }
  145.     
  146.     // set the action for this request
  147.     public final void setAction(int actionType) throws IllegalArgumentException
  148.     {
  149.         if (actionType < SEARCH || actionType > RANDOM)
  150.             throw new IllegalArgumentException("Specified action was not in the proper range.");
  151.         fAction = actionType;
  152.     }
  153.     
  154.     // set the maximun number of records which may be retrieved
  155.     public final void setMaxRecords(int max)
  156.     {    fMaxRecords = max;    }
  157.     
  158.     // set the id of the record this request will be used to retreive
  159.     public final void setRecordID(int i)
  160.     {    fRecordID = i;    }
  161.     
  162.     // set the number of records which will be skipped when Lasso
  163.     // retreives the search results
  164.     // you can retreive records in sets of X by setting this value 
  165.     // to the value of maxRecords from the previous request
  166.     // and submitting this request again
  167.     public final void setSkipRecords(int i)
  168.     {    fSkipRecords = i;    }
  169.     
  170.     // set the global logical operator
  171.     public final void setLogicalOperator(int i)
  172.     {    
  173.         if (i == OR)
  174.             fLogicalOperator = OR;
  175.         else 
  176.             fLogicalOperator = AND;
  177.     }
  178.     
  179.     // returns the global logical operator
  180.     public final int logicalOperator()
  181.     {    return fLogicalOperator;    }
  182.     
  183.     // set the maximum number of seconds Lasso will wait for 
  184.     // a response from the database
  185.     public final void setTimeout(int i)
  186.     {    fTimeout = i;        }
  187.     
  188.     // returns the timeout value
  189.     public final int timeout()
  190.     {    return fTimeout;        }
  191.     
  192.     // returns the maxumum number of records which will be retrieved    
  193.     public final int maxRecords()
  194.     {    return fMaxRecords;    }
  195.     
  196.     // returns the number of records which will be skipped when the request is submitted
  197.     public final int skipRecords()
  198.     {    return fSkipRecords;    }
  199.     
  200.     // returns the ID of the record this request will operate on
  201.     // this is only needs when operating on a single record - an ADD, UPDATE or getting the detail on a 
  202.     // single record
  203.     public final int recordID()
  204.     {    return fRecordID;    }
  205.     
  206.     // returns the action for this request
  207.     public final int action()
  208.     {    return fAction;    }
  209.     
  210.     // add a specific field to be returned.
  211.     // if no return fields are specified, all fields are returned. if any return field is specified, 
  212.     // only the specified fields will be returned
  213.     public void addReturnField( String fieldName )
  214.     {
  215.         fReturnFieldNames.addElement( new String(fieldName) );
  216.     }
  217.     
  218.     // add a field and value to be use in a search
  219.     // uses default operator BEGINS_WITH
  220.     public void addField( String fieldName, String fieldValue )
  221.     {
  222.         addField( fieldName, fieldValue, BEGINS_WITH );
  223.     }
  224.     
  225.     // add a field and value to be used in a search along with the operator 
  226.     public synchronized void addField( String fieldName, String fieldValue, int fieldOperator )
  227.     {    
  228.         if ( ( fieldName  != null ) && ( fieldName.length()  > 0 ) && validFieldOperator( fieldOperator ) )
  229.         {
  230.             Integer    fieldOperatorObj = new Integer( fieldOperator );
  231.             
  232.             fSearchFieldNames.addElement( new String( fieldName ) );
  233.             
  234.             if ( ( fieldValue != null ) && ( fieldValue.length() > 0 ) )
  235.             {
  236.                 fSearchFieldValues.addElement( new String( fieldValue ) );
  237.             }
  238.             else
  239.             {
  240.                 fSearchFieldValues.addElement( new String() );
  241.             }
  242.                 
  243.             fSearchFieldOperators.addElement( fieldOperatorObj );
  244.         }
  245.     }
  246.     
  247.     // remove a field from the field lists
  248.     // this will remove all values for the field
  249.     // returns the number of values which were removed
  250.     public synchronized int removeField(String name)
  251.     {
  252.         boolean done = false;
  253.         int    removed = 0;
  254.         do
  255.         {
  256.             try
  257.             {
  258.                 Enumeration enum = fSearchFieldNames.elements();
  259.                 boolean    foundOne = false;
  260.                 for (int itemNum = 0; enum.hasMoreElements() && !foundOne; ++itemNum)
  261.                 {
  262.                     String    current = (String)enum.nextElement();
  263.                     if (current.equals(name))
  264.                     {
  265.                         foundOne = true;
  266.                         fSearchFieldNames.removeElementAt(itemNum);
  267.                         fSearchFieldValues.removeElementAt(itemNum);
  268.                         fSearchFieldOperators.removeElementAt(itemNum);
  269.                         ++removed;
  270.                     }
  271.                 }
  272.                 if (!foundOne) done = true;
  273.             }
  274.             catch(NoSuchElementException e)
  275.             {    System.err.println(e.toString()); done = true;    }
  276.             catch(ArrayIndexOutOfBoundsException e)
  277.             {    System.err.println(e.toString()); done = true;    }
  278.             
  279.         } while (!done);
  280.         
  281.         return removed;
  282.     }
  283.     
  284.     // set the logical operator for the fields added after this
  285.     public void startOperator(int op)
  286.     {
  287.         if (op == LassoRequest.OR)
  288.             addField("[op_begin]", "or");
  289.         else
  290.             addField("[op_begin]", "and");
  291.     }
  292.     
  293.     // end the last logical operator
  294.     public void endOperator()
  295.     {    addField("[op_end]", " ");        }
  296.     
  297.     public final boolean validFieldOperator( int fieldOperator )
  298.     {
  299.         return ( ( fieldOperator >= EQUALS ) && ( fieldOperator <= LESS_THAN_EQUALS ) );
  300.     }
  301.     
  302.     // add sort criteria along with a sort order
  303.     public synchronized void addSortField( String fieldName, int sortOrder )
  304.     {
  305.         if ( ( fieldName != null ) && ( fieldName.length() > 0 ) && validSortOrder( sortOrder ) )
  306.         {
  307.             Integer sortOrderObj = new Integer( sortOrder );
  308.             
  309.             fSortFields.addElement( new String( fieldName ) );
  310.             fSortOrders.addElement( sortOrderObj );
  311.         }
  312.     }
  313.     
  314.     public final boolean validSortOrder( int sortOrder )
  315.     {
  316.         if ( ( sortOrder >= ASCENDING ) && ( sortOrder <= CUSTOM ) )
  317.             return true;
  318.         
  319.         return false;
  320.     }
  321.     
  322.     // creates the param string which can be submitted to Lasso
  323.     public String toString()
  324.     {
  325.         StringBuffer fileSpecBuffer = new StringBuffer();
  326.         try
  327.         {
  328.             validate();
  329.                     
  330.             fileSpecBuffer.append( fLasso + "?[response]=[nohtml]" );
  331.             
  332.             fileSpecBuffer.append( "&[database]=" );
  333.             
  334.             fileSpecBuffer.append( LassoProxy.prepare( fDatabaseName ) );
  335.                 
  336.             if ( fLayoutName != null )
  337.             {
  338.                 fileSpecBuffer.append( "&[layout]=" );
  339.                 fileSpecBuffer.append( LassoProxy.prepare( fLayoutName ) );
  340.             }
  341.             
  342.             if ( fAction == SEARCH || fAction == FIND_ALL)
  343.             {
  344.                 if ( fSkipRecords > 0 )
  345.                 {
  346.                     fileSpecBuffer.append( "&[skiprecords]=" );
  347.                     fileSpecBuffer.append( fSkipRecords );
  348.                 }
  349.             
  350.                 if ( fMaxRecords == ALL )
  351.                 {
  352.                     fileSpecBuffer.append( "&[maxrecords]=all" );
  353.                 }
  354.                 else if ( fMaxRecords > 0 )
  355.                 {
  356.                     fileSpecBuffer.append( "&[maxrecords]=" );
  357.                     fileSpecBuffer.append( fMaxRecords );
  358.                 }
  359.             
  360.                 if ( fTimeout > 0 )
  361.                 {
  362.                     fileSpecBuffer.append( "&[timeout]=" );
  363.                     fileSpecBuffer.append( fTimeout );
  364.                 }
  365.             
  366.                 if ( fLogicalOperator == OR )
  367.                     fileSpecBuffer.append( "&[logicalop]=or" );
  368.             }
  369.         }
  370.         catch ( Exception e )
  371.         {
  372.             System.out.println( "Exception: " + e );
  373.             return null;
  374.         }
  375.         try
  376.         {
  377.             if ( fSearchFieldNames != null )
  378.             {
  379.                 for ( int i = 0; i < fSearchFieldNames.size(); i++ )
  380.                 {
  381.                     if ( fAction == SEARCH )
  382.                     {
  383.                         fileSpecBuffer.append( "&[op]=" );
  384.                 
  385.                         switch ( ((Integer)fSearchFieldOperators.elementAt( i )).intValue() )
  386.                         {
  387.                             case EQUALS :                fileSpecBuffer.append( "eq" );     break;
  388.                             case NOT_EQUALS :            fileSpecBuffer.append( "neq" );    break;
  389.                             case CONTAINS :                fileSpecBuffer.append( "cn" );    break;
  390.                             case BEGINS_WITH :            fileSpecBuffer.append( "bw" );    break;
  391.                             case ENDS_WITH :            fileSpecBuffer.append( "ew" );    break;
  392.                             case GREATER_THAN :            fileSpecBuffer.append( "gt" );    break;
  393.                             case GREATER_THAN_EQUALS :    fileSpecBuffer.append( "gte" );    break;
  394.                             case LESS_THAN :            fileSpecBuffer.append( "lt" );    break;
  395.                             case LESS_THAN_EQUALS :        fileSpecBuffer.append( "lte" );    break;
  396.                     
  397.                             default : fileSpecBuffer.append( "bw" );
  398.                         }
  399.                     }
  400.                 
  401.                     fileSpecBuffer.append( "&" );
  402.                     fileSpecBuffer.append( LassoProxy.prepare( (String)fSearchFieldNames.elementAt( i ) ) );
  403.                     fileSpecBuffer.append( "=" );
  404.                     fileSpecBuffer.append( LassoProxy.prepare( (String)fSearchFieldValues.elementAt( i ) ) );
  405.                 }
  406.             }
  407.             
  408.             if (fReturnFieldNames != null)
  409.             {
  410.                 for (int i = 0; i < fReturnFieldNames.size(); ++i)
  411.                 {
  412.                     fileSpecBuffer.append("&[returnfield]=");
  413.                     fileSpecBuffer.append(LassoProxy.prepare((String)fReturnFieldNames.elementAt(i)));
  414.                 }
  415.             }
  416.             
  417.             if ( fSortFields != null )
  418.             {
  419.                 for ( int i = 0; i < fSortFields.size(); i++ )
  420.                 {
  421.                     fileSpecBuffer.append( "&[sortfield]=" );
  422.                     fileSpecBuffer.append( LassoProxy.prepare( (String)fSortFields.elementAt( i ) ) );
  423.                     fileSpecBuffer.append( "&[sortorder]=" );
  424.                     
  425.                     switch ( ((Integer)fSortOrders.elementAt( i )).intValue() )
  426.                     {
  427.                         case ASCENDING :    fileSpecBuffer.append( "ascend" );    break;
  428.                         case DESCENDING :    fileSpecBuffer.append( "descend" );    break;
  429.                         case CUSTOM :        fileSpecBuffer.append( "custom" );    break;
  430.                     
  431.                         default : fileSpecBuffer.append( "ascend" );
  432.                     }
  433.                 }
  434.             }
  435.         }
  436.         catch ( Exception e )
  437.         {
  438.             System.out.println( "Exception: " + e );
  439.             return null;
  440.         }
  441.         
  442.         if ( fRecordID >= 0 )
  443.         {
  444.             fileSpecBuffer.append( "&[recid]=" );
  445.             fileSpecBuffer.append( fRecordID );
  446.         }
  447.             
  448.         switch ( fAction )
  449.         {
  450.             case ADD :              fileSpecBuffer.append( "&[add]" );    break;
  451.             case UPDATE :          fileSpecBuffer.append( "&[update]" );    break;
  452.             case DELETE :          fileSpecBuffer.append( "&[delete]" );    break;
  453.             case FIELD_INFO :    fileSpecBuffer.append( "&[show]" );    break;
  454.             case FIND_ALL:         fileSpecBuffer.append( "&[findall]" );     break;
  455.             case RANDOM:        fileSpecBuffer.append("&[random]");     break;
  456.             
  457.             default : fileSpecBuffer.append( "&[search]" ); // default is search
  458.         }
  459.         
  460.         return fileSpecBuffer.toString();
  461.     }
  462.     
  463.     public void validate() throws DatabaseNameException
  464.     {
  465.         if ( fDatabaseName == null ) throw ( new DatabaseNameException() );
  466.     }
  467. }
  468.  
  469. class DatabaseNameException extends Exception
  470. {
  471.     public DatabaseNameException()    {};
  472.     
  473.     public String toString()
  474.     {
  475.         return ( "LassoRequest: database name not provided." );
  476.     }
  477. }
  478.  
  479.